package com.iqiyi.pps.epg.web.webapp.action;

import com.iqiyi.kiwi.utils.JsonUtil;
import com.iqiyi.pps.epg.web.utils.SqlTimestampConverter;
import com.iqiyi.pps.epg.core.model.auth.SysUser;
import com.iqiyi.pps.epg.core.utils.SpringSecurityUtils;
import com.iqiyi.pps.epg.web.utils.RequestUtils;
import com.iqiyi.pps.epg.web.utils.Struts2Utils;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.DateConverter;
import org.apache.struts2.convention.annotation.Action;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.text.MessageFormat;

/**
 * Struts2中典型CRUD Action的抽象基类.
 * <p/>
 * 主要定义了对Preparable,ModelDriven接口的使用,以及CRUD函数和返回值的命名.
 *
 * @param <T> CRUDAction所管理的对象类型.
 * @author calvin
 */
public abstract class BaseAction<T> extends ActionSupport implements ModelDriven<T>, Preparable {

    private static final long serialVersionUID = -1653204626115064950L;

    static {
        ConvertUtils.register(new DateConverter(null), java.util.Date.class);
        ConvertUtils.register(new SqlTimestampConverter(null), Timestamp.class);
    }
    
    /**
     * 进行增删改操作后,以redirect方式重新打开action默认页的result名.
     */
    public static final String RELOAD = "reload";
    protected Logger logger = LoggerFactory.getLogger(getClass());
    protected HttpServletRequest servletRequest;
    public String act = "view";
    private String logInfo;
    protected String code;
    protected String message;


    /**
     * Action函数, 默认的action函数, 默认调用list()函数.
     */
    @Override
    public String execute() throws Exception {
		return SUCCESS;
    }

    //-- CRUD Action函数 --//

    /**
     * Action函数,显示Entity列表界面.
     * 建议return SUCCESS.
     */
    public abstract String list() throws Exception;

    /**
     * Action函数,显示新增或修改Entity界面.
     * 建议return INPUT.
     */
    @Override
    public abstract String input() throws Exception;

    /**
     * Action函数,新增或修改Entity.
     * 建议return RELOAD.
     */
    public abstract String save() throws Exception;

    /**
     * Action函数,删除Entity.
     * 建议return RELOAD.
     */
    public abstract String delete() throws Exception;

    /**
     * Action函数,查看Entity.
     * 建议return INPUT.
     */
    @Action(value = "view")
    public String view() throws Exception {
        return SUCCESS;
    }
    //-- Preparable函数 --//

    /**
     * 实现空的prepare()函数,屏蔽了所有Action函数都会执行的公共的二次绑定.
     */
    public void prepare() throws Exception {
         prepareModel();
    }

    /**
     * 定义在input()前执行二次绑定.
     */
    public void prepareInput() throws Exception {
        prepareModel();
    }

    /**
     * 定义在save()前执行二次绑定.
     */
    public void prepareSave() throws Exception {
        prepareModel();
    }

    /**
     * 定义在save()前执行二次绑定.
     */
    public void prepareView() throws Exception {
        prepareModel();
    }
    /**
     * 等同于prepare()的内部函数,供prepardMethodName()函数调用.
     */
    protected abstract void prepareModel() throws Exception;

    public String getAct() {
        return act;
    }

    public void setAct(String act) {
        this.act = act;
    }

    public String getIp(){
       // HttpServletRequest servletRequest = ServletActionContext.getRequest();
        servletRequest = Struts2Utils.getRequest();
        String ip = RequestUtils.getRemoteAddr(servletRequest);
        return ip;
    }

    public HttpServletRequest getServletRequest() {
        return servletRequest;
    }

    public void setServletRequest(HttpServletRequest servletRequest) {
        this.servletRequest = servletRequest;
    }
    
    public SysUser getCurrentUser() {
        return (SysUser) SpringSecurityUtils.getCurrentUser();
    }

     public String getLogInfo() {
        return logInfo;
    }

    public void setLogInfo(String logInfo) {
        this.logInfo = logInfo;
    }

    protected static final String simpleajaxMessage = "\"code\":\"{0}\",\"message\":\"{1}\"";

    public void renderAjaxMessage(String result, String message) {
        String json = "{" + MessageFormat.format(simpleajaxMessage, new String[]{result, message}) + "}";
        System.out.println(json);
        Struts2Utils.renderText(json);
    }

    protected static final String ajaxMessage = "\"code\":\"{0}\",\"message\":\"{1}\",\"data\":{2}";
    public void renderAjaxMessage(String result, String message, Object... data) {
        String dataStr= JsonUtil.formatObject(data);
        String json = "{" + MessageFormat.format(ajaxMessage, new String[]{result, message, dataStr}) + "}";
        System.out.println(json);
        Struts2Utils.renderText(json);
    }
}
